<?php 
	
	class Common_model extends CI_Model 
	{
	
		function record_exist($table_name, $where)
		{
			$sql = "SELECT count(*) as cnt FROM {$table_name} WHERE ".$where;
			$res = $this->db->query($sql)->row_array();
			$res = (int)$res['cnt'];
			if( $res > 0 )	{
				return TRUE;
			}else{
				return FALSE;	
			}
		}
		
		function get_found_rows()
		{
			$sql = "SELECT FOUND_ROWS() as cnt";
			$result = $this->db->query($sql)->row_array();
			return (int) $result['cnt'];
		}
		
		function replace_spaces($name)
		{
			$name = preg_replace('/\s+/','-',$name);
			return $name;
		}
		
		function getAllLoactions()
		{
			$sql = "SELECT * FROM locations ORDER BY name";
			return $this->db->query($sql)->result();
		}
		
		function getAllContext()
		{
			$sql = "select DISTINCT context from sip_buddies where context is not null and context <> '' order by context";
			return $this->db->query($sql)->result();
		}
		
		function getVdnLocations()
		{
			$sql = "select DISTINCT location from vdn where location is not null and location  <> ''  order by location  ";
			return $this->db->query($sql)->result();
		}
		
		function getModules()
		{
			$sql = "select * from modules where active = 1 order by ordering ASC";
			return $this->db->query($sql)->result();
		}
		
		function getUserModules($user_id)
		{
			$user_type = $this->session->userdata('sess_user_type');
			if( $user_type == 'admin' )
			{	
				return $this->getModules();
			}
			else
			{
				$user_id = (int)$user_id;
				$sql = "select  m.id, m.name, m.url, mp.permission 
					FROM modules m INNER JOIN modules_permission mp ON m.id = mp.module_id
					WHERE m.active = 1 AND mp.user_id = ".$user_id." ORDER BY ordering";
			}
			return $this->db->query($sql)->result();		
		}
		
		function check_module($module_url, $permission)
		{
			$user_type = $this->session->userdata('sess_user_type');
			$user_id = $this->session->userdata('sess_user_id');
			if( $user_type == 'admin' ) {
				return true;	
			}
			
			if( $module_url == '' || $permission == '' )
			{
				return FALSE;	
			}
			
			$where = '';
			if( preg_match("/,/", $permission) ) // check for multiple permission, if module need either 'add' or 'edit' we provide add,edit as second parameter
			{
				$arr_permis = array();
				$permission_parts = explode(",", $permission);
				foreach($permission_parts as $per) 
				{
					$arr_permis[] = " FIND_IN_SET('".$per."', mp.permission) ";
				}
				$where = implode("OR", $arr_permis);
				$where = " ( ".$where. " ) ";
			}
			else
			{
				$where = " FIND_IN_SET('".$permission."', mp.permission) ";	
			}
			
			$sql = "select  count(*) as cnt
					FROM modules m INNER JOIN modules_permission mp ON m.id = mp.module_id
					WHERE 
						m.active = 1 AND 
						mp.user_id = ".$user_id." AND 
						m.url = ".$this->db->escape($module_url)." AND {$where}
			";		

			$res = $this->db->query($sql)->row_array();							
			$res = (int)$res['cnt'];
			if( $res > 0 )	
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
			
		}
		
		function check_permission($module_url, $permission)
		{
			if( ! $this->check_module($module_url, $permission) ) 
			{
				//$this->load->view('access_denied'); 
				redirect('/access_denied');	
			}	
		}
		
		function DB_modification_logs($module)
		{
			$user_id = $this->session->userdata('sess_user_id');
			$query   = $this->db->last_query();	
			
			if( $this->db->affected_rows() > 0 )
			{
				if( preg_match('/INSERT INTO/', $query) )
				{
					$action = 'add';	
				}
				elseif( preg_match('/DELETE FROM/', $query) )
				{
					$action = 'delete';	
				} 
				else
				{
					$action = 'update';		
				}
				
				$record  = array(
									'module' => $module,
									'query'  => addslashes($query),
									'action' => $action,
									'user_id' => $user_id,
									'time' => date('Y-m-d H:i:s')
								);
				
				$this->db->insert("db_modification_logs", $record);	
			}
		}
		
		function trackUser()
		{
			$session_id = $this->session->userdata('session_id');
			$user_id 	= $this->session->userdata('sess_user_id');
			if($user_id>0)
			{
				$uri = $this->uri->segment(1);
				if( $this->uri->segment(2) != '' ){
					$uri .= '/'. $this->uri->segment(2);	
				}
				
				$sql="Select uri from user_log where user_id=$user_id and session_id="."'".$session_id."'";
				$result=$this->db->query($sql)->row();
				$array = @explode(',',$result->uri);
				if( ! empty($array[0]) ) 
				{
					if(!in_array($uri,$array))
					{
						$uri=$result->uri.",".$uri;
						$this->db->where('user_id',$user_id);
						$this->db->where('session_id',$session_id);
						$record=array( 'uri'=>$uri);
							$this->db->update("user_log", $record);
					}			
				}
				else
				{
					$this->db->where('user_id',$user_id);
					$this->db->where('session_id',$session_id);
					$record=array( 'uri'=>$uri );
					$this->db->update("user_log", $record);
				}
			}
		}
		
		function userLog($ip,$browser)
		{
			$user_id 	= $this->session->userdata('sess_user_id');
			$username	= $this->session->userdata('sess_username');
			$fullname	= $this->session->userdata('sess_full_name');
			$session_id	= $this->session->userdata('session_id');
			$userType	= $this->session->userdata('sess_user_type');
			$record = array(
							'user_id' 	=> $user_id,
							'username' 	=> $username,
							'full_name'=>$fullname,
							'login_at' => date('Y-m-d H:i:s'),
							'user_type' =>$userType,
							'browser'=>$browser,
							'ip' => $ip,
							'session_id'=>$session_id
						);
			$this->db->insert("user_log", $record);			
 		}
		
		function getAllTimeZones()
		{
			$sql = "select * from timezones where active = 1 order by offset";
			return $this->db->query($sql)->result();
		}
  }
?>